﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using Common;

namespace HTMLSolution
{
    /// <summary>
    /// 百度音乐Api接口，使用逻辑封装
    /// </summary>
    public class BaiDuMusic
    {
        /// <summary>
        /// 搜索的当前页的结果数据
        /// </summary>
        List<MusicSearchData> _result = null;
        public BaiDuMusic()
        {
            _result = new List<MusicSearchData>();
        }
        /// <summary>
        /// 获取指定页面的搜索数据
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <returns></returns>
        public List<MusicSearchData> LoadPage(string qword, int pageIndex = 1)
        {
            List<MusicSearchData> list = new List<MusicSearchData>();
            HtmlWeb web = new HtmlWeb();
            HtmlDocument doc = web.Load(GetSearchUrl(qword, pageIndex));
            //1.获取maintable
            HtmlNode main = doc.DocumentNode.SelectSingleNode("//table[@class='mainTable']");
            main = HtmlNode.CreateNode(main.OuterHtml);
            //2.获取所有列表项
            //获取列表，去除class=all和class=tableRule 的tr节点
            HtmlNodeCollection col = main.SelectNodes("//tr[not(contains(@class,'all')) and not(contains(@class,'tableRule'))]");
            foreach (HtmlNode item in col)
            {

                list.Add(GetItem(item));
            }
            return list;
        }
        /// <summary>
        /// 解析单项
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private MusicSearchData GetItem(HtmlNode node)
        {
            node = HtmlNode.CreateNode(node.OuterHtml);
            MusicSearchData data = new MusicSearchData();
            //1.歌ID和歌名
            try
            {
                HtmlNode name = node.SelectSingleNode("//td[contains(@class,'sName')]/div/a");
                if (name == null)
                {
                    data.SongName = "获取歌名失败";
                    data.SongID = "";
                }
                else
                {
                    data.SongName = name.InnerText;
                    string id = name.Attributes["onclick"].Value;
                    id = SecurityHelper.HtmlDecode(id);
                    int start = id.IndexOf("'");
                    int end = id.IndexOf("'", start + 1);
                    data.SongID = id.Substring(start, (end - start));
                }
            }
            catch (Exception ex)
            {

            }
            //2.歌手
            try
            {
                HtmlNode uName = node.SelectSingleNode("//td[contains(@class,'uName')]/div/a");
                if (uName == null)
                {
                    data.Singer = "获取歌手失败";
                }
                else
                {
                    data.Singer = uName.InnerText;
                    data.SingerLink = uName.Attributes["href"].Value;
                }
            }
            catch (Exception)
            {

            }
            //3.专辑
            try
            {
                HtmlNode aName = node.SelectSingleNode("//td[contains(@class,'aName')]/a");
                if (aName == null)
                {
                    data.Special = "获取专辑失败";
                }
                else
                {
                    data.Special = aName.InnerText;
                    data.Special = aName.Attributes["href"].Value;
                }
            }
            catch (Exception)
            {

            }

            return data;
        }
        /// <summary>
        /// 获取搜索链接
        /// </summary>
        /// <param name="qword">搜索的歌手或歌名</param>
        /// <param name="pageIndex">页索引</param>
        /// <returns></returns>
        public string GetSearchUrl(string qword, int pageIndex)
        {
            string url = string.Format("http://musicmini.baidu.com/app/search/searchList.php?qword={0}&ie=utf-8&page={1}",
                qword, pageIndex);
            return url;
        }
    }
    /// <summary>
    /// 查询结果数据
    /// </summary>
    public class MusicSearchData
    {
        public string SongID { get; set; }

        public string SongName { get; set; }

        public string Singer { get; set; }

        public string SingerLink { get; set; }

        public string Special { get; set; }
        public string SpecialLink { get; set; }
    }
}
